<?php
/*................................................................
[七行 PHPCMS]
 .  VERSION: 1.0
 .  Author: SevenEl sevenelement@qq.com
 .  Support: http://www.sevenel.cn
................................................................*/
class Db
{
private $host;
private $user;
private $pwd;
private $dbname;
private $ConnId;
private $db_selected;
private $result;
private $prefix;
private $new_link;
function __construct($name){
	$dbcfg=Sys::Gp(array('dbcfg',$name));
	$this->host = $dbcfg['host'];
	$this->user = $dbcfg['user'];
	$this->pwd = $dbcfg['psw'];
	$this->prefix = $dbcfg['prefix'];
	$this->dbname = $dbcfg['database'];
	$this->new_link = $dbcfg['new_link'];
	$this->ConnId = mysql_connect($this->host,$this->user,$this->pwd,$this->new_link);
	if(!$this->ConnId){
		die('Sorry,Connect database false! '.mysql_error());
		}else{
		$this->db_selected = mysql_select_db($this->dbname,$this->ConnId);
		if(!$this->db_selected){
			die('Cannot select the database:'.mysql_error());
			}
		$this->result = mysql_query("set names '".$dbcfg['encode']."';");
		}
	}

function __destruct(){
	@mysql_free_result();
	@mysql_close($this->ConnId);
	}
function Desql($sql){//datebasExecuteSQL
	$this->result = mysql_query($sql,$this->ConnId);
	if(!$this->result){
		die('Cannot update the database: '.mysql_error());
		}else{
		return $this->result;
		}
	}
function Dqsql($sql){//database QuerySQL
	$i = 0;
	$result_arr = array();
	$this->result = mysql_query($sql,$this->ConnId);
	if(!$this->result){
        Jc($sql);
		die('Cannot view the database: '.mysql_error());
		}else{
		while($row = mysql_fetch_array($this->result)){
			$result_arr[$i++] = $row;
			}
		return $result_arr;
		mysql_free_result($this->result);
		}
	}

function Dqcfg($tname){
	$i = 0;
	$result_arr = array();
	//echo $this->prefix;
	//echo $this->ConnId;
	$sql="select * from ".$this->prefix."_".$tname." where 1 limit 1 ";
	//echo $sql;
	$this->result = mysql_query($sql,$this->ConnId);
	if($this->result){
		while($i <  mysql_num_fields($this->result)){
			$result_arr[$i] = mysql_field_name($this->result,$i);
			$i++;
			}
		return $result_arr;
		mysql_free_result($this->result);
	}
}
function getc($name){
			if($name=='pwd'){
				exit;
				}
			return $this->$name;
	}


}

/*
$obj = new Db(array ('host' => 'localhost','user' => 'root', 'psw' => '123456','database'=> 'bibi','prefix' => 'lm', 'encode' => 'utf8' ));
$result_arr=$obj->QuerySQL("select * from lm_ad where 1 limit 3");
print_r($result_arr);
*/
?>
